var app=getApp()
 
Page({

  /**
   * 页面的初始数据
   */
  data: {
   teacherArray:[],
   studentArray:[],
   telePhone:'',
   show:'',
   style: 'width:123rpx;height40rpx;margin-left:90rpx;', 
    // subArray: [{
    //   id: 'yuwen',
    //   value: '语文'
    // },
    // {
    //   id: 'shuxue',
    //   value: '数学'
    // },
    // {
    //   id: 'yingyu',
    //   value: '英语'
    // }
    //   ,
    // {
    //   id: 'wuli',
    //   value: '物理'
    // }
    //   ,
    // {
    //   id: 'huaxue',
    //   value: '化学'
    // },
    // {
    //   id: 'shengwu',
    //   value: '生物'
    // },
    // {
    //   id: 'lishi',
    //   value: '历史'
    // },
    //  {
    //   id: 'dili',
    //   value: '地理'
    //  },
    //  {
    //   id: 'zhengzhi',
    //   value: '政治'
    //  }, 
    // {
    //   id: 'qita',
    //   value: '其他'
    // }
    // ],
    subArray:[],
    subDisplay:'none',
    zheHeight: '',
    move:true,
    shoWhat:true,
    tanchuDis:false,
    title:'',
    name:'',
    nickName:'',
    sex:'',
    address: '',
    grade:'',
    subject:'',
    teacherSex:'',
    yaoqiu:'',
    salar:'',
    teachTime:'',
    pageNo:2,
    qingkuang:''
    
  },
  changeSubject:function(event){
    this.setData({
      subDisplay: 'none',
    
    })
    var that=this
    console.log(event)
    var id=event.currentTarget.dataset.id
    var name = event.currentTarget.dataset.name
    var show=this.data.shoWhat
    if(show){
      this.getTeacherByName(name,1)
    }else if(!show){
      console.log(name)
      this.getStudentByName(name,1)
     
    }
      
  
  },
  getTeacherByName:function(name,pageNo){
    var that=this
    this.setData({
      teacherArray:[]
    })
    wx.request({
      url: app.globalData.baseUrl + '/demand/demand/query',
      data: {
        courseName: name,
        type: 2,
        pageNo: 1,
        pageSize: 30
      },
      method: 'get',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log('根据teachername搜索')
        console.log(res)
        try {
          for (var index = (pageNo - 1) * 10; index < (pageNo - 1) * 10 + res.data.data.data.length; index++) {
            that.setData({
              teacherArray:''
            })
            var name = 'teacherArray[' + index + '].name'
            var id = 'teacherArray[' + index + '].id'
            var sex = 'teacherArray[' + index + '].sex'
            var course = 'teacherArray[' + index + '].course'
            var position = 'teacherArray[' + index + '].position'
            var xuanyan='teacherArray['+index+'].xuanyan'
            var address = 'teacherArray[' + index + '].address'
            var avatarUrl = 'teacherArray[' + index + '].avatarUrl'
            var danwei = 'teacherArray[' + index + '].danwei'
            var juli = 'teacherArray[' + index + '].juli'
            var courseTime = 'teacherArray[' + index + '].course_time'
            var key='teacherArray['+index+'].key'
            
            var titleTeacher = res.data.data.data[index].title

            var distance = res.data.data.data[index].distance
            distance = (distance - 1).toLocaleString();
            distance = distance.toString().replace(/\$|\,/g, '');
            distance = parseInt(distance)

            var danweizz
            if (distance > 1000) {
              danweizz = 'km'

              distance = parseInt(distance / 1000)
            } else {
              danweizz = 'm'
            }
            if (titleTeacher.length > 7) {
              titleTeacher = titleTeacher.substring(0, 6) + '...'
            } else {

            }
            var sexTeacher = ''
            if (res.data.data.data[index].teacherGender == '0' || res.data.data.data[index].gender == 0) {
              sexTeacher = true
            } else {
              sexTeacher = false
            }
            var lesson = res.data.data.data[index].course.name
            if(lesson>12){
              lesson=lesson.substring(0,10)+'...'
            }
          
            that.setData({
             
              [danwei]: danweizz, 
              [juli]: distance,
              [name]: titleTeacher,
              [avatarUrl]: res.data.data.data[index].userAvatarUrl,
              [address]:res.data.data.data[index].position,
              [sex]: sexTeacher,

              [id]: res.data.data.data[index].teacher.id,
              [course]:lesson,
               [xuanyan]:res.data.data.data[index].special,
              [key]:'上课地点'
            })
        
          }
        } catch (err) {
          console.log(err)
          wx.showToast({
            title: '暂时没有更多数据',
            icon: 'none'
          })
        }
       console.log(that.data.teacherArray)
     
      }

    })
  },
  getStudentByName:function(name){
    var that=this
    this.setData({
      studentArray:[]
    })
    wx.request({
      url: app.globalData.baseUrl + '/demand/demand/query',
      data: {
        courseName: name,
        type: 1,
        pageNo:1,
        pageSize:30
      },
      method: 'get',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        that.setData({
          studentArray:''
        })
        console.log('根据学生name搜索')
        console.log(res)
        try {
          if(res.data.data.data.length==0){
            wx.showToast({
              mask:true,
              icon:'none',
              title: '暂时没有更多数据',
            })
          }
          for (var index = 0; index < res.data.data.data.length; index++) {
            var type = res.data.data.data[index].type
            console.log(type)
            var distanceType = false
            if (type == 3) {
              distanceType = true
            }
            console.log(distanceType)
            var avatarUrl = 'studentArray[' + index + '].avatarUrl'
            var sex = 'studentArray[' + index + '].sex'
            var nickName = 'studentArray[' + index + '].nickName'
            var title = 'studentArray[' + index + '].title'
            var salar = 'studentArray[' + index + '].price'
            var id = 'studentArray[' + index + '].id'
            var position = 'studentArray[' + index + '].position'
            var course = 'studentArray[' + index + '].course'
            var subject = 'studentArray[' + index + '].subject'
            var time = 'studentArray[' + index + '].time'

            var showDistance = 'studentArray[' + index + '].showDistance'
            var stutitle = res.data.data.data[index].title
            if (stutitle.length > 10) {
              stutitle = stutitle.substring(0, 10) + '..'
            }
            var distance = res.data.data.data[index].distance
            distance = (distance - 1).toLocaleString();
            distance = distance.toString().replace(/\$|\,/g, '');
            distance = parseInt(distance)
            // console.log(distance)
            // console.log(typeof distance)
            var danweizz
            if (distance > 1000) {
              danweizz = 'km'
              // var distanceStr = distance.toString().
              // distance=distanceStr.substring(0,distance.length-3)
              distance = parseInt(distance / 1000)
            } else {
              danweizz = 'm'
            }
            // console.log(danweizz)
            // console.log(distance)
            var studentNickname = res.data.data.data[index].userNickName
            if (studentNickname.length > 8) {
              studentNickname = studentNickname.substring(0, 6) + '...'
            }
            var juli = 'studentArray[' + index + '].juli'
            var danwei = 'studentArray[' + index + '].danwei'
            that.setData({

              [avatarUrl]: res.data.data.data[index].userAvatarUrl,
              [showDistance]: distanceType,
              [juli]: distance,
              [danwei]: danweizz,
              [nickName]: studentNickname,
              [id]: res.data.data.data[index].id,
              [sex]: res.data.data.data[index].user.gender,
              [salar]: parseInt(res.data.data.data[index].price),
              [time]: res.data.data.data[index].special,

              [subject]: res.data.data.data[index].course.name,
              [position]: res.data.data.data[index].position,
              [title]: stutitle

            })


          }
        } catch (err) {
          console.log(err)
          wx.showToast({
            title: '没有相关数据',
            icon: 'none'
          })
        }
      }

    })
  },
  contactParent:function(event){
    var that=this
    var id=event.currentTarget.dataset.id
    var show=this.data.show
    console.log(show)
    var vip=wx.getStorageSync('vip')
    if(show){
      if(vip){
        console.log(that.data.telePhone)
        wx.showModal({
          title: '',
          content: '您每天有两次免费联系对方的机会',
          success:function(res){
            if(res.confirm){
              wx.makePhoneCall({
                phoneNumber: that.data.telePhone,
                success: function (res) {

                  console.log(res)
                  console.log('拨打电话成功')
                },
                fail: function (err) {
                  console.log(that.data.telePhone)
                  console.log(err)

                }
              })
            }
          
          }
        })
      
      }else {
        wx.showModal({
          title: '',
          content: '您不是会员无法联系对方,是否成为会员?',
          success:function(res){
            if(res.confirm){
              wx.navigateTo({
                url: '/geren/becomeVIP/becomeVIP',
              })
            }
          }
        })
      }
    
    }else{
      console.log(event)
      console.log("contact")
      wx.navigateTo({
        url: '/xiaoxi/chat/chat?id=' + id,
      })
    }
  
  },
  toSelect:function(res){
    var that=this
    // this.setData({
    //   shoWhat:!this.data.shoWhat
    // })
    console.log(res)
    var id=res.detail.id
    if(id=='one'){
        this.setData({
          shoWhat:true
        })
     }else if(id=='two')
    {
      this.setData({
        shoWhat: false
      })
    }else{
      if (this.data.subDisplay == 'none') {
        this.setData({
          subDisplay: 'block',
          move: !this.data.move
        })
      } else {
        this.setData({
          move: !this.data.move,
          subDisplay: 'none'
        })
      }


    }
    wx.request({
      
      url: app.globalData.baseUrl+'demand/listall',
      data:{
        type:1,
       
      },
      method:'GET',
      header:{
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID'),
      },
      success:function(res){
        console.log('res')
        console.log(res)
        that.setData({
          studentArray:[]
        })
        try{
          for (var index = 0; index < res.data.data.data.length; index++) {
            var type=res.data.data.data[index].type
            console.log(type)
            var distanceType=false
            if(type==3){
                distanceType=true
            }
            console.log(distanceType)
            var avatarUrl = 'studentArray[' + index + '].avatarUrl'
            var sex = 'studentArray[' + index + '].sex'
            var nickName = 'studentArray[' + index + '].nickName'
            var title = 'studentArray[' + index + '].title'
            var salar = 'studentArray[' + index + '].price'
            var id = 'studentArray[' + index + '].id'
            var position = 'studentArray[' + index + '].position'
            var course = 'studentArray[' + index + '].course'
            var subject = 'studentArray[' + index + '].subject'
            var time='studentArray['+index+'].time'
           
            var showDistance = 'studentArray[' + index + '].showDistance'
            var stutitle = res.data.data.data[index].title
            if (stutitle.length >10) {
              stutitle = stutitle.substring(0, 10) + '..'
            }
            var distance=res.data.data.data[index].distance
            distance=(distance-1).toLocaleString();
            distance=distance.toString().replace(/\$|\,/g,'');
            distance=parseInt(distance)
            // console.log(distance)
            // console.log(typeof distance)
            var danweizz
            if(distance>1000){
              danweizz='km'
              // var distanceStr = distance.toString().
              // distance=distanceStr.substring(0,distance.length-3)
              distance=parseInt(distance/1000)
            }else{
              danweizz='m'
            }
            // console.log(danweizz)
            // console.log(distance)
            var studentNickname = res.data.data.data[index].userNickName
            if (studentNickname.length > 8) {
              studentNickname = studentNickname.substring(0,6) + '...'
            }
            var juli='studentArray['+index+'].juli'
           var danwei='studentArray['+index+'].danwei'
            that.setData({
              
              [avatarUrl]: res.data.data.data[index].userAvatarUrl,
              [showDistance]:distanceType,
              [juli]:distance,
              [danwei]:danweizz,
              [nickName]: studentNickname,
              [id]: res.data.data.data[index].id,
              [sex]: res.data.data.data[index].gender,
              [salar]:parseInt(res.data.data.data[index].price),
              [time]:res.data.data.data[index].special,
             
              [subject]: res.data.data.data[index].courseName,
              [position]:res.data.data.data[index].position,
              [title]: stutitle

            })


          }
        }catch(err){
          console.log(err)
            wx.showToast({
              title: '获取数据失败',
              icon:'none'
            })
        }
     
      }
    })
   
  },
  getMoreTeacherList:function(pageNo){
    
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'demand/listall',
      method: 'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID'),


      },
      data: {
        pageNo:pageNo,
        pageSize:10,
        type: 2
      },
      success: function (res) {
        //  var that =this
        console.log('teacher')
        console.log(res)
     try{
       for (var index = (pageNo - 1) * 10; index < (pageNo - 1) * 10 + res.data.data.data.length; index++) {
         var name = 'teacherArray[' + index + '].name'
         var id = 'teacherArray[' + index + '].id'
         var sex = 'teacherArray[' + index + '].sex'
         var course = 'teacherArray[' + index + '].course'
         var position = 'teacherArray[' + index + '].address'
         var xuanyan = 'teacherArray[' + index + '].xuanyan'
         var avatarUrl = 'teacherArray[' + index + '].avatarUrl'
         var danwei = 'teacherArray[' + index + '].danwei'
         var juli = 'teacherArray[' + index + '].juli'
         var courseTime = 'teacherArray[' + index + '].course_time'
         var key='teacherArray['+index+'].key'
         var titleTeacher = res.data.data.data[index].title

         var distance = res.data.data.data[index].distance
         distance = (distance - 1).toLocaleString();
         distance = distance.toString().replace(/\$|\,/g, '');
         distance = parseInt(distance)

         var danweizz
         if (distance > 1000) {
           danweizz = 'km'

           distance = parseInt(distance / 1000)
         } else {
           danweizz = 'm'
         }
         if (titleTeacher.length > 7) {
           titleTeacher = titleTeacher.substring(0, 6) + '...'
         } else {

         }
         var sexTeacher = ''
         if (res.data.data.data[index].teacherGender == '0' || res.data.data.data[index].gender == 0) {
           sexTeacher = true
         } else {
           sexTeacher = false
         }

         var pos = res.data.data.data[index].position
         if (pos.length > 8) {
           pos = pos.substring(0, 7) + '...'
         }
         that.setData({

           [danwei]: danweizz,
           [juli]: distance,
           [name]: titleTeacher,
           [avatarUrl]: res.data.data.data[index].userAvatarUrl,
           [sex]: sexTeacher,
           [position]: pos,
           [id]: res.data.data.data[index].teacherId,
           [course]: res.data.data.data[index].courseName.substring(0, 10),
           [key]:"上课安排",
            [xuanyan]:res.data.data.data[index].special

         })

       }
     }catch(err){

     }
        
        console.log(that.data.teacherArray)

      },
      fail: function (errmsg) {
        wx.showToast({
          title: errmsg,
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var height
  
     wx.getSystemInfo({
       success: function(res) {
         height=res.windowHeight
       },
     })
     this.setData({
       zheHeight:height
     })
   
  },
  getContent:function(event){
      var that=this
      this.setData({
        tanchuDis:!this.data.tanchuDis
      })
      console.log(event)
      var id=event.currentTarget.dataset.id
      var show=event.currentTarget.dataset.show
      console.log(show)
      console.log(id)
      wx.request({
        url: app.globalData.baseUrl+'demand/demand/'+id,
        method:'GET',
        header:{
          'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
        },
        success:function(res){
          console.log(res)
          if(res.data.code==666){
            wx.showModal({
              title: '',
              content: '积分不足请充值',
              success:function(res){
                if(res.confirm){
                  wx.navigateTo({
                    url: '/geren/chongzhiPage/chongzhiPage',
                  })
                }
              }
            })
          }else{
            try {
              try {
                var name;
                if (res.data.data.title == '') {
                  name = '该用户未填写姓名'
                } else {
                  name = res.data.data.title
                }
                var add
                if (res.data.data.position == '') {
                  add = '该用户未填写地址'
                } else {
                  add = res.data.data.position
                }
                var userSex = res.data.data.user.gender
                var userSexString
                if (userSex == 0) {
                  userSexString = '男'
                } else if (userSex == 1) {
                  userSexString = '女'
                } 
                var teacherSex = res.data.data.teacherGender
                var teacherSexString = ''
                if (teacherSex == 0) {
                  teacherSexString = '男'
                } else if (teacherSex == 1) {
                  teacherSexString = '女'
                } else {
                  teacherSexString = '无所谓'
                }
                that.setData({
                  title: res.data.data.title,
                  name: name,
                  address: add,
                  show: show,
                  nickName:res.data.data.user.nickName,
                  parentID: res.data.data.userId,
                  grade: res.data.data.grade.name,
                  subject: res.data.data.course.name,
                  yaoqiu: res.data.data.description,
                  teacherSex: teacherSexString,
                  teachTime: res.data.data.special,
                  qingkuang: res.data.data.content,
                  sex:userSexString,
                  salar: res.data.data.price
                })
              } catch (err) {
                console.log(err)
                wx.showToast({
                  title: '获取数据失败',
                  icon: 'none'
                })
              }
              if (that.data.show) {
                console.log('机构发布的需求有电话')
                console.log(res.data.data.userTelephone)
                that.setData({
                  telePhone: res.data.data.userTelephone,
                })
              }
            } catch (err) {
              that.onShow()
            }
          }
         
          }
         
      })
  },
  changeTanchu:function(event){
    this.setData({
      tanchuDis:false,
      
    })
  },
  toTeacher:function(event){
    console.log(event)
    var id=event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/shouye/teacher/teacher?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面
   * .
   * 初次渲染完成
   */
  onReady: function () {
    var that=this
    wx.request({
      url: app.globalData.baseUrl + 'demand/listcourse',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log("教师类型")
        console.log(res)
        var resData=res.data.data.data
        for(var index=0;index<resData.length;index++){
          var id='subArray['+index+'].id'
          var value='subArray['+index+'].value'
          that.setData({
            [id]:resData[index].id,
            [value]:resData[index].name
          })

        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.isLogin()
    this.getMoreTeacherList(1)
    //  this.setData({
    //    subDisplay: 'none',
    //    zheHeight: '',
    //    move: true,
    //    shoWhat: true,
    //    tanchuDis: false
    //  })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      tanchuDis: !this.data.tanchuDis
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMoreTeacherList(this.data.pageNo)
    this.setData({
      pageNo:this.pageNo+1
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})